home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Internet Info 1994 March
/
Internet Info CD-ROM (Walnut Creek) (March 1994).iso
/
networking
/
ip
/
ka9q
/
src890906.arc
/
ICMP.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-08-13
|
6KB
|
235 lines
/* Internet Control Message Protocol */
#include <stdio.h>
#include "global.h"
#include "mbuf.h"
#include "internet.h"
#include "timer.h"
#include "iface.h"
#include "ip.h"
#include "icmp.h"
#include "netuser.h"
#include "tcp.h"
struct icmp_errors Icmp_errors;
struct icmp_stats Icmp_stats;
/* Process an incoming ICMP packet */
void
icmp_input(bp,ip,rxbroadcast)
struct mbuf *bp; /* Pointer to ICMP message */
struct ip *ip;
int rxbroadcast;
{
struct mbuf *tbp;
struct icmp icmp; /* ICMP header */
struct ip oip; /* Offending datagram header */
int16 type; /* Type of ICMP message */
int16 length;
if(rxbroadcast){
/* Broadcast ICMP packets are to be IGNORED !! */
Icmp_errors.bdcsts++;
free_p(bp);
return;
}
length = ip->length - IPLEN - ip->optlen;
if(cksum(NULLHEADER,bp,length) != 0){
/* Bad ICMP checksum; discard */
Icmp_errors.checksum++;
free_p(bp);
return;
}
ntohicmp(&icmp,&bp);
/* Process the message. Some messages are passed up to the protocol
* module for handling, others are handled here.
*/
type = icmp.type;
if(type < ICMP_TYPES)
Icmp_stats.input[type]++;
switch(uchar(type)){
case TIME_EXCEED: /* Time-to-live Exceeded */
case DEST_UNREACH: /* Destination Unreachable */
case QUENCH: /* Source Quench */
ntohip(&oip,&bp); /* Extract offending IP header */
if(Icmp_trace){
printf("ICMP from %s:",inet_ntoa(ip->source));
printf(" dest %s %s",inet_ntoa(oip.dest),
smsg(Icmptypes,ICMP_TYPES,uchar(type)));
switch(uchar(type)){
case TIME_EXCEED:
printf(" %s\n",
smsg(Exceed,NEXCEED,uchar(icmp.code)));
break;
case DEST_UNREACH:
printf(" %s\n",
smsg(Unreach,NUNREACH,uchar(icmp.code)));
break;
default:
printf(" %u\n",uchar(icmp.code));
break;
}
}
switch(uchar(oip.protocol)){
case TCP_PTCL:
tcp_icmp(oip.source,oip.dest,icmp.type,icmp.code,&bp);
break;
}
break;
case ECHO: /* Echo Request */
/* Change type to ECHO_REPLY, recompute checksum,
* and return datagram.
*/
icmp.type = ECHO_REPLY;
if((tbp = htonicmp(&icmp,bp)) == NULLBUF){
free_p(bp);
return;
}
Icmp_stats.output[ECHO_REPLY]++;
ip_send(ip->dest,ip->source,ICMP_PTCL,ip->tos,0,tbp,length,0,0);
return;
case REDIRECT: /* Redirect */
case PARAM_PROB: /* Parameter Problem */
break;
case ECHO_REPLY: /* Echo Reply */
echo_proc(ip->source,ip->dest,&icmp,bp);
bp = NULLBUF; /* so it won't get freed */
break;
case TIMESTAMP: /* Timestamp */
case TIME_REPLY: /* Timestamp Reply */
case INFO_RQST: /* Information Request */
case INFO_REPLY: /* Information Reply */
break;
}
free_p(bp);
}
/* Return an ICMP response to the sender of a datagram.
* Unlike most routines, the callER frees the mbuf.
*/
int
icmp_output(ip,data,type,code,args)
struct ip *ip; /* Header of offending datagram */
struct mbuf *data; /* Data portion of datagram */
char type,code; /* Codes to send */
union icmp_args *args;
{
struct mbuf *bp;
struct icmp icmp; /* ICMP protocol header */
int16 dlen; /* Length of data portion of offending pkt */
int16 length; /* Total length of reply */
if(ip == NULLIP)
return -1;
if(uchar(ip->protocol) == ICMP_PTCL){
/* Peek at type field of ICMP header to see if it's safe to
* return an ICMP message
*/
switch(uchar(data->data[0])){
case ECHO_REPLY:
case ECHO:
case TIMESTAMP:
case TIME_REPLY:
case INFO_RQST:
case INFO_REPLY:
break; /* These are all safe */
default:
/* Never send an ICMP error message about another
* ICMP error message!
*/
Icmp_errors.noloop++;
return -1;
}
}
if(type < ICMP_TYPES)
Icmp_stats.output[type]++;
/* Compute amount of original datagram to return.
* We return the original IP header, and up to 8 bytes past that.
*/
dlen = min(8,len_mbuf(data));
length = dlen + ICMPLEN + IPLEN + ip->optlen;
/* Take excerpt from data portion */
if(data != NULLBUF && dup_p(&bp,data,0,dlen) == 0)
return -1; /* The caller will free data */
/* Recreate and tack on offending IP header */
if((data = htonip(ip,bp)) == NULLBUF){
free_p(bp);
return -1;
}
icmp.type = type;
icmp.code = code;
switch(uchar(icmp.type)){
case PARAM_PROB:
icmp.args.pointer = args->pointer;
break;
case REDIRECT:
icmp.args.address = args->address;
break;
case ECHO:
case ECHO_REPLY:
case INFO_RQST:
case INFO_REPLY:
case TIMESTAMP:
case TIME_REPLY:
icmp.args.echo.id = args->echo.id;
icmp.args.echo.seq = args->echo.seq;
break;
default:
icmp.args.unused = 0;
break;
}
/* Now stick on the ICMP header */
if((bp = htonicmp(&icmp,data)) == NULLBUF){
free_p(data);
return -1;
}
return ip_send(Ip_addr,ip->source,ICMP_PTCL,ip->tos,0,bp,length,0,0);
}
/* Generate ICMP header in network byte order, link data, compute checksum */
struct mbuf *
htonicmp(icmp,data)
struct icmp *icmp;
struct mbuf *data;
{
struct mbuf *bp;
register char *cp;
int16 checksum;
if((bp = pushdown(data,ICMPLEN)) == NULLBUF)
return NULLBUF;
cp = bp->data;
*cp++ = icmp->type;
*cp++ = icmp->code;
cp = put16(cp,0); /* Clear checksum */
cp = put16(cp,icmp->args.echo.id);
cp = put16(cp,icmp->args.echo.seq);
/* Compute checksum, and stash result */
checksum = cksum(NULLHEADER,bp,len_mbuf(bp));
cp = &bp->data[2];
cp = put16(cp,checksum);
return bp;
}
/* Pull off ICMP header */
int
ntohicmp(icmp,bpp)
struct icmp *icmp;
struct mbuf **bpp;
{
char icmpbuf[8];
if(icmp == (struct icmp *)NULL)
return -1;
if(pullup(bpp,icmpbuf,8) != 8)
return -1;
icmp->type = icmpbuf[0];
icmp->code = icmpbuf[1];
icmp->args.echo.id = get16(&icmpbuf[4]);
icmp->args.echo.seq = get16(&icmpbuf[6]);
return 0;
}